package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.SaleCommissionDto;
import com.ymatou.iapi.productmanager.parameter.UpdateCatalogCommisionsBean;
import com.ymatou.iapi.productmanager.parameter.UpdateGiftTypeBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.UpdateCatalogCommisionsCall;
import com.ymatou.iapi.productmanager.service.UpdateGiftTypeCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.*;
import org.junit.experimental.categories.Category;

/**
*升级礼包类型
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateGiftType{
	private static UpdateGiftTypeBean updategifttypeBean;
	private static UpdateGiftTypeCall updategifttypeCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("globalSellerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("升级礼包类型");
	}

	@Before
	public void caseUp() {
		updategifttypeBean=new UpdateGiftTypeBean();
		updategifttypeCall=new UpdateGiftTypeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updategifttypeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void updatecatalogcommisions(String productId,AddSellerProductRequest addSellerProductBean) throws InterruptedException, ClientProtocolException, IOException {
		UpdateCatalogCommisionsBean updatecatalogcommisionsBean=new UpdateCatalogCommisionsBean();
		UpdateCatalogCommisionsCall updatecatalogcommisionsCall=new UpdateCatalogCommisionsCall();
		updatecatalogcommisionsBean.setProductId(productId);
		updatecatalogcommisionsBean.setSellerName(addSellerProductBean.getSellerName());
		updatecatalogcommisionsBean.setUserId(addSellerProductBean.getSellerId());
		SaleCommissionDto saleCommissionDto = new SaleCommissionDto();
		//gift 0 是单个 1 2是多个
		List<YmtCatalogsI> catalogslist =  new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
		saleCommissionDto.setCatalogId(catalogslist.get(0).getsCatalogId());
		BigDecimal saleCommission1 = new BigDecimal(5.00);
		BigDecimal saleCommission2 = new BigDecimal(6.00);
		BigDecimal saleCommission3 = new BigDecimal(7.00);
		BigDecimal saleCommission4 = new BigDecimal(8.00);
		saleCommissionDto.setSaleCommissionList(Arrays.asList(saleCommission1,saleCommission2,saleCommission3,saleCommission4));
		updatecatalogcommisionsBean.setCommissionList(Arrays.asList(saleCommissionDto));
		saleCommissionDto.setSaleCommission(saleCommission1);
		updatecatalogcommisionsCall.setData(updatecatalogcommisionsBean);
		updatecatalogcommisionsCall.callService();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateGiftType_001() {
		Logger.start(true,"升级礼包类型,1礼包商品");
		try {
			AddSellerProductRequest addSellerProductBean = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductBean).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updatecatalogcommisions(productId, addSellerProductBean);
			
			updategifttypeBean.setProductId(productId);
			updategifttypeBean.setUserId(addSellerProductBean.getSellerId());
			updategifttypeBean.setGiftType(1);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("200",updategifttypeCall.getString("Code"),"验证Code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
//			new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(c->{
//				Logger.debug(FormatUtil.beanToGSONString(c));
//			});
			Logger.verifyEquals(1,ymtProductsI.getGiftType(),"验证礼包类型");
			
			updatecatalogcommisions(productId, addSellerProductBean);
//			new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(c->{
//				Logger.debug(FormatUtil.beanToGSONString(c));
//			});
			updategifttypeBean.setGiftType(0);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("200",updategifttypeCall.getString("Code"),"验证Code");
			ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(0,ymtProductsI.getGiftType(),"验证礼包类型");
			new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(c->{
				Logger.debug(FormatUtil.beanToGSONString(c));
				Logger.verifyEquals("0", c.getSaleCommission().toBigInteger().toString(), "验证Commission");
				Logger.verifyEquals(null, c.getSaleCommissions(), "验证Commissions");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateGiftType_002() {
		Logger.start(true,"升级礼包类型，买手不是101");
		try {
			AddSellerProductRequest addSellerProductBean = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductBean).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updategifttypeBean.setProductId(productId);
			updategifttypeBean.setUserId(addSellerProductBean.getSellerId());
			updategifttypeBean.setGiftType(1);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("201",updategifttypeCall.getString("Code"),"验证Code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(0,ymtProductsI.getGiftType(),"验证礼包类型");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateGiftType_003() {
		Logger.start(true,"升级礼包类型，商品已是礼包商品");
		try {
			AddSellerProductRequest addSellerProductBean = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductBean).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updategifttypeBean.setProductId(productId);
			updategifttypeBean.setUserId(addSellerProductBean.getSellerId());
			updategifttypeBean.setGiftType(1);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("200",updategifttypeCall.getString("Code"),"验证Code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(1,ymtProductsI.getGiftType(),"验证礼包类型");
			updategifttypeCall.callService();
			Logger.verifyEquals("201",updategifttypeCall.getString("Code"),"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateGiftType_004() {
		Logger.start(true,"升级礼包类型，商品不存在");
		try {
			AddSellerProductRequest addSellerProductBean = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductBean).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//商品不存在
			updategifttypeBean.setProductId("jwehfiowjj");
			updategifttypeBean.setUserId(addSellerProductBean.getSellerId());
			updategifttypeBean.setGiftType(1);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("201",updategifttypeCall.getString("Code"),"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateGiftType_005() {
		Logger.start(true,"升级礼包类型,2优选团商品");
		try {
			AddSellerProductRequest addSellerProductBean = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductBean).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updategifttypeBean.setProductId(productId);
			updategifttypeBean.setUserId(addSellerProductBean.getSellerId());
			updategifttypeBean.setGiftType(2);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("200",updategifttypeCall.getString("Code"),"验证Code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(2,ymtProductsI.getGiftType(),"验证礼包类型");
			updategifttypeBean.setGiftType(0);
			updategifttypeCall.setData(updategifttypeBean);
			updategifttypeCall.callService();
			Logger.verifyEquals("200",updategifttypeCall.getString("Code"),"验证Code");
			ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(0,ymtProductsI.getGiftType(),"验证礼包类型");
			new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(c->{
				Logger.debug(FormatUtil.beanToGSONString(c));
				Logger.verifyEquals("0", c.getSaleCommission().toBigInteger().toString(), "验证Commission");
				Logger.verifyEquals(null, c.getSaleCommissions(), "验证Commissions");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}